package com.hs.hbp.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;
import com.hs.core.utils.UploadfileUtil;
import com.hs.hbp.service.IHbpZhuService;
@Controller  
public class FileUploadController extends AbstractAction {
	private static final Logger logger = Logger.getLogger(FileUploadController.class);
	private IHbpZhuService hbpZhuService;
	 @Autowired   
	 public void setHbpZhuService(IHbpZhuService hbpZhuService) {
		this.hbpZhuService = hbpZhuService;
	}
	/**
	  * 上传文件
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/uploadfile.do")   
	public ModelAndView uploadfile(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		UploadfileUtil uploadfileUtil = new UploadfileUtil();
		File destfile =  uploadfileUtil.uploadFile(request, "D:/work/destdir/");
		return new ModelAndView("test/uploadFile");  
	}
	
	/**
	  * 下载文件
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/downloadfile.do")   
	public ModelAndView downloadfile(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		System.out.println("下载文件开始");
		try  {
			String path = "D:/work/destdir/诚信证明.doc" ;
           //  path是指欲下载的文件的路径。 
          File file  =   new  File(path);
          String filename  =  file.getName();
         //  取得文件的后缀名。 
          String ext  =  filename.substring(filename.lastIndexOf( " . " )  +   1 ).toUpperCase();
          InputStream fis  =   new  BufferedInputStream( new  FileInputStream(path));
           byte [] buffer  =   new   byte [fis.available()];
          fis.read(buffer);
          fis.close();
          response.reset();
          System.out.println("下载文件开始" +   new  String(filename.getBytes("utf-8"),"iso8859-1")); 
          response.addHeader( " Content-Disposition " ,  " attachment;filename=aaa.doc "   );
          response.addHeader( " Content-Length " ,  ""   +  file.length());
          OutputStream toClient  =   new  BufferedOutputStream(response.getOutputStream());
          response.setContentType( "application/msword" );
          toClient.write(buffer);
          toClient.flush();
          toClient.close();
          
      }  catch  (IOException ex) {
          ex.printStackTrace();
      }
		return new ModelAndView("test/downloadFile");  
	} 
	
	/**
	  * 上传并解析pbh文件,.txt文件,
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/uploadPbhCN.do")   
	public ModelAndView uploadPbhCN(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		System.out.println("解析文件开始"); 
		UploadfileUtil uploadfileUtil = new UploadfileUtil();
		File destfile =  uploadfileUtil.uploadFile(request, "D:/work/destdir/");
		System.out.println("上传文件成功"); 
		hbpZhuService.parseCnHbp(destfile);
		return new ModelAndView("test/uploadFile");  
	}

	/**
	  * 上传并解析文件.txt,英文文件中文标题
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/uploadPbhENCN.do")   
	public ModelAndView uploadPbhENCN(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		System.out.println("上传文件开始"); 
		UploadfileUtil uploadfileUtil = new UploadfileUtil();
		File destfile =  uploadfileUtil.uploadFile(request, "D:/work/destdir/");
		System.out.println("上传文件结束" + destfile.toString()); 
		hbpZhuService.parseEnCnHbp(destfile);
		return new ModelAndView("test/uploadFile");  
	}
	
	/**
	  * 上传并解析文件.txt,英文文件英文标题
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/uploadPbhENEN.do")   
	public ModelAndView uploadPbhENEN(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		System.out.println("上传文件开始"); 
		UploadfileUtil uploadfileUtil = new UploadfileUtil();
		File destfile =  uploadfileUtil.uploadFile(request, "D:/work/destdir/");
		System.out.println("上传文件结束" + destfile.toString()); 
		hbpZhuService.parseEnEnHbp(destfile);
		return new ModelAndView("test/uploadFile");  
	}
	/**
	  * 导入文件，中文
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/importPbhCN.do")   
	public ModelAndView importPbhCN(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		System.out.println("导入文件开始"); 
		hbpZhuService.importCnFile();
		System.out.println("导入文件结束"); 
		return new ModelAndView("app/test/uploadFile");  
	}
	
	/**
	  * 导入文件，英文文件中文标题
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/importPbhENCN.do")   
	public ModelAndView importPbhENCN(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		System.out.println("导入文件开始"); 
		hbpZhuService.importEnCnFile();
		System.out.println("导入文件结束"); 
		return new ModelAndView("app/test/uploadFile");  
	}
	
	/**
	  * 导入文件，英文文件英文标题
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/importPbhENEN.do")   
	public ModelAndView importPbhENEN(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		System.out.println("导入文件开始"); 
		hbpZhuService.importEnEnFile();
		System.out.println("导入文件结束"); 
		return new ModelAndView("app/test/uploadFile");  
	}
}
